//
//  HomePageCommentListCell.m
//  ShowYork
//
//  Created by hxh on 2020/11/19.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "HomePageReplyMainTitleCell.h"
#import "TTTAttributedLabel.h"
#import "HomePageCommentUserInfoModel.h"
#import "HomePageFeedbackView.h"


@interface HomePageReplyMainTitleCell ()<TTTAttributedLabelDelegate>
@property (nonatomic,strong)UIImageView *headerIcon;
@property (nonatomic,strong)UILabel *nameLb;
@property (nonatomic,strong)TTTAttributedLabel *contentLb;
@property (nonatomic,strong)UILabel *createDateLb;

@property (nonatomic,strong)UILabel *likeNumLb;
@property (nonatomic,strong)UIImageView *commentImgView;
@property (nonatomic,strong)UIImageView *likeImgView;
@property (nonatomic,strong)UIView *line;

@property (nonatomic,strong)UIButton *likebtn;
@property (nonatomic,strong)UIButton *sendCommentBtn;

@property (nonatomic,strong)UIButton *contentCoverBtn;
@end

@implementation HomePageReplyMainTitleCell


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUI];
    }
    return self;
}
- (void)setUI{
        
    self.headerIcon = [[UIImageView alloc]init];
    self.headerIcon.image = [UIImage imageNamed:@"defaut_header"];
    self.headerIcon.layer.cornerRadius = 16;
    self.headerIcon.layer.masksToBounds = YES;
    [self.contentView addSubview:self.headerIcon];
    
    [self.headerIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.contentView).offset(16);
        make.size.mas_equalTo(CGSizeMake(32, 32));
    }];
    
    self.nameLb = [[UILabel alloc]init];
    self.nameLb.text = @"吃土少年";
    self.nameLb.textColor = C102;
    self.nameLb.font = font(14);
    [self.contentView addSubview:self.nameLb];
    [self.nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(16);
        make.left.equalTo(self.headerIcon.mas_right).offset(12);
    }];
    
    self.contentLb = [[TTTAttributedLabel alloc]initWithFrame:CGRectZero];
    self.contentLb.textColor = RGBA(51, 51, 51, 1);
    self.contentLb.delegate = self;
    self.contentLb.numberOfLines = 0;
    self.contentLb.font = [UIFont systemFontOfSize:14];
    self.contentLb.linkAttributes = @{(NSString *)kCTForegroundColorAttributeName:[RPAppManager themColor],
                                    (NSString *)kCTUnderlineStyleAttributeName:[NSNumber numberWithBool:NO]};
    [self.contentView addSubview:self.contentLb];
    
    [self.contentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLb.mas_bottom).offset(2);
        make.right.equalTo(self.contentView).offset(-16);
        make.left.equalTo(self.nameLb);
    }];
  
    self.createDateLb = [[UILabel alloc]init];
    self.createDateLb.text = @"2020-08-06 06:21";
    self.createDateLb.textColor = RGBA(153, 153, 153, 1);
    self.createDateLb.font = font(10);
    [self.contentView addSubview:self.createDateLb];
    [self.createDateLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLb.mas_bottom).offset(12);
        make.left.equalTo(self.nameLb);
        make.bottom.equalTo(self.contentView).offset(-16);
    }];
        
    self.likeNumLb = [[UILabel alloc]init];
    self.likeNumLb.text = @"1";
    self.likeNumLb.textColor = C153;
    self.likeNumLb.font = font(14);
    [self.contentView addSubview:self.likeNumLb];
    [self.likeNumLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-18);
        make.centerY.equalTo(self.createDateLb);
    }];
    
    self.likeImgView = [[UIImageView alloc]init];
    self.likeImgView.image = [UIImage imageNamed:@"home_comment_like_tag"];
    self.likeImgView.highlightedImage = [UIImage imageNamed:@"home_likeSelected_tag"];
    [self.contentView addSubview:self.likeImgView];
    [self.likeImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.likeNumLb.mas_left).offset(-8);
        make.centerY.equalTo(self.createDateLb);
    }];
    
    self.commentImgView = [[UIImageView alloc]init];
    self.commentImgView.image = [UIImage imageNamed:@"home_comment_tag"];
    [self.contentView addSubview:self.commentImgView];
    [self.commentImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.likeImgView.mas_left).offset(-24);
        make.centerY.equalTo(self.createDateLb);
    }];
    
    self.line = [[UIView alloc]init];
    self.line.backgroundColor = RGBA(230, 230, 230, 1);
    [self.contentView addSubview:self.line];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.equalTo(self.contentView);
        make.left.equalTo(self.nameLb);
        make.height.mas_equalTo(0.5);
    }];
    
    self.likebtn = [[UIButton alloc]init];
    [self.likebtn addTarget:self action:@selector(likeMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.likebtn];
    [self.likebtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.likeImgView);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
    
    
    self.sendCommentBtn = [[UIButton alloc]init];
    [self.sendCommentBtn addTarget:self action:@selector(sendCommentMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.sendCommentBtn];
    [self.sendCommentBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.commentImgView);
        make.size.mas_equalTo(CGSizeMake(30, 30));
        
    }];

    self.headerIcon.userInteractionEnabled = YES;
    UITapGestureRecognizer *headerIconTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showUserInfo)];
    [self.headerIcon addGestureRecognizer:headerIconTap];

    self.nameLb.userInteractionEnabled = YES;
    UITapGestureRecognizer *nameLbTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showUserInfo)];
    [self.nameLb addGestureRecognizer:nameLbTap];
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapMethod)];
    tap.delegate = self;
    [self.contentLb addGestureRecognizer:tap];
    
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(showMoreView:)];
    longPress.delegate = self;
    [self.contentLb addGestureRecognizer:longPress];
    
    tap.cancelsTouchesInView = NO;
    
    self.contentCoverBtn = [[UIButton alloc]init];
    [self.contentCoverBtn addTarget:self action:@selector(contentCoverBtnMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.contentLb addSubview:self.contentCoverBtn];
    [self.contentCoverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.contentLb);
    }];

}
- (void)showMoreView:(UIGestureRecognizer *)gesture{
    if (gesture.state == UIGestureRecognizerStateBegan) {
        BOOL isMine = [self.model.userId isEqualToNumber:RPAppManager.getCustomerId];
        HomePageFeedbackView *feedbackView = [[HomePageFeedbackView alloc]initWithCompleteBlock:^{
            if (isMine) {
                if (self.delegate && [self.delegate respondsToSelector:@selector(removePostCommentById:)]) {
                    [self.delegate removePostCommentById:self.model.id];
                }
            }else{
                if (self.delegate && [self.delegate respondsToSelector:@selector(reportPostCommentById:)]) {
                    [self.delegate reportPostCommentById:self.model.id];
                }
            }
        }];
        feedbackView.title = isMine?@"删除":@"投诉";
        [feedbackView show];
    }
}

- (void)tapMethod{
    if (self.delegate && [self.delegate respondsToSelector:@selector(sendCommentById:)]) {
        [self.delegate sendCommentById:self.model.id];
    }
}
- (void)contentCoverBtnMethod{
    
}

- (void)showUserInfo{
    if (self.delegate && [self.delegate respondsToSelector:@selector(showUserInfoById:)]) {
        [self.delegate showUserInfoById:self.model.userId];
    }
}
- (void)likeMethod{

    if (![RPAppManager isLogin]) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(doDissOrLikeWithStatus:commentId:)]) {
            [self.delegate doDissOrLikeWithStatus:YES commentId:@0];
        }
        return;
    }
    NSInteger count = self.model.likeNum.intValue;
    if (self.model.like) {
        count = count - 1;
    }else{
        count = count + 1;
    }
    self.model.like = !self.model.like;
    self.model.likeNum = @(count);
    self.likeImgView.highlighted = self.model.like;
    self.likeNumLb.text = [NSString stringWithFormat:@"%lu",count];
    if (self.delegate && [self.delegate respondsToSelector:@selector(doDissOrLikeWithStatus:commentId:)]) {
        [self.delegate doDissOrLikeWithStatus:self.model.like commentId:self.model.id];
    }
}

- (void)sendCommentMethod{
    if (self.delegate && [self.delegate respondsToSelector:@selector(sendCommentById:)]) {
        [self.delegate sendCommentById:self.model.id]; 
    }
}

+(instancetype)cellWithTableView:(UITableView *)tableView{
    NSString *ID = @"HomePageReplyMainTitleCell";
    HomePageReplyMainTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[HomePageReplyMainTitleCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

- (void)setModel:(HomePageCommentItemModel *)model{

    _model = model;
    self.contentLb.text = model.commentContent.commentText;
    self.nameLb.text = model.realName;
    self.createDateLb.text = model.createTimeStr;
    if ([model.userId isEqualToNumber:[RPAppManager getCustomerId]]) {
        UserInfoModel *userInfo = [UserInfoModel userInfo];
        if (userInfo.imagePath.length == 0) {
            if (userInfo.sex.intValue == 0) {
                self.headerIcon.image = [UIImage imageNamed:@"defaut_header"];
            }else if (userInfo.sex.intValue == 1){
                self.headerIcon.image = [UIImage imageNamed:@"common_boy_tag"];
            }else{
                self.headerIcon.image = [UIImage imageNamed:@"common_girl_tag"];
            }
        }else{
            [self.headerIcon sd_setImageWithURL:[NSURL URLWithString:userInfo.imagePath] placeholderImage:[UIImage imageNamed:@"defaut_header"]];
        }
    }else{
        if (model.imagePath.length == 0) {
            if (model.sex.intValue == 0) {
                self.headerIcon.image = [UIImage imageNamed:@"defaut_header"];
            }else if (model.sex.intValue == 1){
                self.headerIcon.image = [UIImage imageNamed:@"common_boy_tag"];
            }else{
                self.headerIcon.image = [UIImage imageNamed:@"common_girl_tag"];
            }
        }else{
            [self.headerIcon sd_setImageWithURL:[NSURL URLWithString:model.imagePath] placeholderImage:[UIImage imageNamed:@"defaut_header"]];
        }

    }
    self.likeNumLb.text = model.likeNum.stringValue;
    NSString *commentContent = model.commentContent.commentText;
    
    if (model.commentContent.mentionUserList.count > 0) {
        for (HomePageCommentUserInfoModel *userInfo in model.commentContent.mentionUserList) {
            NSRange range = [commentContent rangeOfString:[NSString stringWithFormat:@"{%@}",userInfo.id]];
            if (range.location != NSNotFound) {
                commentContent = [commentContent stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"{%@}",userInfo.id] withString:[NSString stringWithFormat:@"@%@",userInfo.realName.length == 0?@"":userInfo.realName]options:0 range:range];
                userInfo.location = range.location;
            }

        }
        self.contentLb.text = commentContent;
        for (HomePageCommentUserInfoModel *userInfo in model.commentContent.mentionUserList) {
            if (userInfo.location != -1) {
                NSRange selRange = NSMakeRange(userInfo.location,userInfo.realName.length + 1);
                [self.contentLb addLinkToTransitInformation:@{@"data":userInfo.id} withRange:selRange];
            }
        }
    }
    //
    self.likeNumLb.text = model.likeNum.stringValue;
    self.createDateLb.text = model.createTimeStr;
    self.nameLb.text = model.realName;
    self.likeImgView.highlighted = model.like;
    
    
    
}
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithTransitInformation:(NSDictionary *)components {
    NSNumber *userId = components[@"data"];
    if (self.delegate && [self.delegate respondsToSelector:@selector(showUserInfoById:)]) {
        [self.delegate showUserInfoById:userId];
    }
}




@end
